<?php
namespace app\artic\controller;
use think\Controller;
use think\Session;
use app\artic\model\ArticleList;
use app\artic\model\Centre;
class Index extends Controller
{
    public function index()
    {
        // var_dump('ok');die;
        if(Session::has('user')){
          $name=Session::get('user');
        }else{
          $name=[];
        }
        $this->assign('name',json_encode($name));
        //列表数据
        $list=ArticleList::where(['id'=>['<>',0]])->order('create_time desc')->paginate('5');
        $page=$list->render();
        foreach($list as $val){
           $val['count']=count($val['content']);
        }
        $this->assign('page',$page);
        $this->assign('list',json_encode($list));
        return $this->fetch();
    }

    public function add(){
    	 return $this->fetch();
    }
    public function update(){
       $id=request()->param('id');
       // echo"<pre>";
       // var_dump($id);die;
       $articinfo= ArticleList::getInformation(['id'=>$id]);
        $articinfo['content']=htmlspecialchars_decode($articinfo['content']);
       $this->assign('actic',json_encode($articinfo));
       return $this->fetch();
    }

    public function info(){
        $id=request()->param('id');
        $articinfo= ArticleList::getInformation(['id'=>$id]);
        $articinfo['content']=htmlspecialchars_decode($articinfo['content']);
        $articinfo['create_time']=date('Y-m-d H:i:s',$articinfo['create_time']);
        $articinfo['update_time']=date('Y-m-d H:i:s',$articinfo['update_time']);
        //文章列表
        $this->assign('actic',json_encode($articinfo));
        //评论列表
        $comments=Centre::getInformationAll([
            'article_id'=>$id,
          ]);
        if($comments){
           foreach($comments as &$val){
            $val['create_time']=date('Y-m-d H:i:s',$val['create_time']);
            $val['update_time']=date('Y-m-d H:i:s',$val['update_time']);
          }
        }
        $this->assign('comments',json_encode($comments));
        return $this->fetch();
    }
}
